//将页面与浏览器访问地址关联
import { createRouter, createWebHistory } from "vue-router";
import Layout from "../layout/Main/Main.vue";

//路由集
const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login.vue"),
  },
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    meta: { requiresAuth: true },
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("../views/home.vue"),
        meta: { showInMenu: true, title: "首页", icon: "House" },
      },
{
        path: "/proxy",
        redirect: "/proxy/proxylist",
        meta: { showInMenu: true, title: "代理管理", icon: "Management" },
        children: [
          {
            path: "/proxylist",
            name: "proxylist",
            component: () => import("../views/ProxyList/ProxyList.vue"),
            meta: { showInMenu: true, title: "代理列表", icon: "Document" },
          },
          {
            path: "/agencyAddressList",
            name: "agencyAddressList",
            component: () =>
              import("../views/AgencyAddressList/AgencyAddressList.vue"),
            meta: { showInMenu: true, title: "代理地址列表", icon: "Location" },
          },
        ],
      },
      {
        path: "/user",
        redirect: "/user/userlist",
        meta: { showInMenu: true, title: "用户管理", icon: "Avatar" },
        children: [
          {
            path: "/balancechange",
            name: "balancechange",
            component: () => import("../views/BalanceChange/BalanceChange.vue"),
            meta: { showInMenu: true, title: "余额变动", icon: "Coin" },
          },
          {
            path: "/userlist",
            name: "userlist",
            component: () => import("../views/UserList/UserList.vue"),
            meta: { showInMenu: true, title: "用户列表", icon: "UserFilled" },
          },
        ],
      },
      
      {
        path: "/penOrder",
        redirect: "/penOrder/penOrderList",
        meta: { showInMenu: true, title: "笔数管理", icon: "List" },
        children: [
          {
            path: "/penOrderList",
            name: "penOrderList",
            component: () => import("../views/PenOrderList/PenOrderList.vue"),
            meta: { showInMenu: true, title: "笔数订单列表", icon: "Tickets" },
          },
          {
            path: "/penUserList",
            name: "penUserList",
            component: () => import("../views/PenUserList/PenUserList.vue"),
            meta: { showInMenu: true, title: "笔数用户列表", icon: "User" },
          },
          {
            path: "/penThirdUserList",
            name: "penThirdUserList",
            component: () =>
              import("../views/PenThirdUserList/PenThirdUserList.vue"),
            meta: {
              showInMenu: true,
              title: "笔数第三方列表",
              icon: "UserFilled",
            },
          },
          {
            path: "/penInfoList",
            name: "penInfoList",
            component: () => import("../views/PenInfoList/PenInfoList.vue"),
            meta: { showInMenu: true, title: "笔数记录列表", icon: "DocumentCopy" },
          },
        ],
      },

      {
        path: "/pendingwithdrawals",
        name: "pendingwithdrawals",
        component: () =>
          import("../views/PendingWithdrawals/PendingWithdrawals.vue"),
        meta: { showInMenu: true, title: "待提现列表", icon: "Money" },
      },

      {
        path: "/fastRentInfoList",
        name: "fastRentInfoList",
        component: () =>
          import("../views/FastRentInfoList/FastRentInfoList.vue"),
        meta: { showInMenu: true, title: "闪租记录列表", icon: "Timer" },
      },
      {
        path: "/fastExchangeInfoList",
        name: "fastExchangeInfoList",
        component: () =>
          import("../views/FastExchangeInfoList/FastExchangeInfoList.vue"),
        meta: { showInMenu: true, title: "闪兑记录列表", icon: "Refresh" },
      },
    ],
  },
];

//路由创建
const router = createRouter({
  history: createWebHistory("/admin/"),
  routes: routes,
});

router.beforeEach((to, from, next) => {
  // 统一 token 的键名
  const tokenKey = window.SystemConstant?.TOKEN_KEY || "authToken";
  const token = localStorage.getItem(tokenKey);

  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title;
  } else {
    document.title = "后台管理系统";
  }

  // 检查路由是否需要认证
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (token) {
      next();
    } else {
      next({ name: "login" });
    }
  } else {
    // 如果是登录页且已登录，则重定向到首页
    if (to.name === "login" && token) {
      next({ name: "home" });
    } else {
      next();
    }
  }
});

router.afterEach(() => {
  // 滚动到页面顶部
  window.scrollTo(0, 0);
});

export default router;
